<?php
/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */
/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}
/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;        
        default:
            $url = U($url);
            break;
    }
    return $url;
}

function getXiaoiInfo($openid, $content)
{
	//定义app
	$app_key="BTG5QLCl5HLX";
	$app_secret="dHRoQ8xpLz3D08GKs1s9";
	//签名算法
	$realm = "xiaoi.com";
	$method = "POST";
	$uri = "/robot/ask.do";
	$nonce = "";
	$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
	for ($i = 0; $i < 40; $i++) {
		$nonce .= $chars[ mt_rand(0, strlen($chars) - 1) ];
	}
	$HA1 = sha1($app_key.":".$realm.":".$app_secret);
	$HA2 = sha1($method.":".$uri);
	$sign = sha1($HA1.":".$nonce.":".$HA2);

	//接口调用
	$url = "http://nlp.xiaoi.com/robot/ask.do";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Auth:    app_key="'.$app_key.'", nonce="'.$nonce.'", signature="'.$sign.'"'));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "question=".urlencode($content)."&userId=".$openid."&platform=custom&type=0");
	$output = curl_exec($ch);
	if ($output === FALSE){
		return "cURL Error: ". curl_error($ch);
	}
	return trim($output);
}
function getLatestPic()
{
	$pics=M('PictureTurn')->order('id desc')->limit(1)->select();
	return $pics[0];
}
function getLatestPics()
{
	$pics=M('PictureTurn')->order('id desc')->limit(3)->select();
	return $pics;
}
function getLatestDoc()
{
	$docs=M('Document')->order('id desc')->limit(1)->select();
	return $docs[0];
}
function getLatestWords()
{

}